package com.quasar.hpatient.module.mine_feedback;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.ImageView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.bean.comm_album.AlbumPhotoBean;
import com.quasar.hpatient.module.comm_album.AlbumActivity;
import com.quasar.hpatient.module.comm_camera.CameraActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.widget.menu.NavigationView;
import lib.quasar.widget.photo.PhotoLayout;
import lib.quasar.widget.photo.PhotoModel;

/**
 * description: 我 ==> 设置 ==> 意见反馈
 * created by kalu on 2018/7/16 14:05
 */
public final class MineFeedbackActivity extends BaseActivity<MineFeedbackPresenter> implements MineFeedbackView {

    public final static int REQUEST_CODE = 1001;

    @Override
    public int initView() {
        return R.layout.activity_mine_feedback;
    }

    @Override
    public void initDataLocal() {

        final NavigationView menu = findViewById(R.id.mine_feedback_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {
            if (isBack) onBackPressed();
            else toast("提交");
        });

        final RecyclerView recycler = findViewById(R.id.mine_feedback_picture);
        getPresenter().initList(this, recycler);
    }

    @Override
    public void showAlbum(List<AlbumPhotoBean> mPhoto) {

        final Intent intent = new Intent(getApplicationContext(), AlbumActivity.class);
        intent.putExtra(AlbumActivity.LIST, (Serializable) mPhoto);
        intent.putExtra(AlbumActivity.CAMERA, true);
        startActivityForResult(intent, REQUEST_CODE);
    }

    @Override
    public void showPhoto(int defaultPosition, List<? extends PhotoModel> imageLittleUrlList, List<? extends PhotoModel> imageUrlList, List<ImageView> imageViews) {

        if (imageLittleUrlList.isEmpty() || imageUrlList.isEmpty() || imageViews.isEmpty())
            return;

        new PhotoLayout.Builder(this)
                .setPhotoLongPressSave(false)
                .setPhotoOpenTransAnim(false)
                .setPhotoDefaultPosition(defaultPosition)
                .setPhotoBackgroundColor(Color.BLACK)
                .setPhotoDefaultResource(R.mipmap.ic_launcher)
                .setPhotoViewList(imageViews)
                .setPhotoUrlList(imageUrlList)
                .setPhotoLittleUrlList(imageLittleUrlList)
                .show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == AlbumActivity.RESULT_CODE_OPEN_CAMERA) {
            Intent intent = new Intent(getApplicationContext(), CameraActivity.class);
            startActivityForResult(intent, REQUEST_CODE);
        } else if (resultCode == AlbumActivity.RESULT_CODE && null != data) {
            ArrayList<AlbumPhotoBean> list = data.getParcelableArrayListExtra(AlbumActivity.LIST);
            if (null == list || list.size() == 0) return;
            final RecyclerView recycler = findViewById(R.id.mine_feedback_picture);
            getPresenter().compressPhoto(this, recycler, list, -1);
        } else if (resultCode == CameraActivity.RESULT_CODE && null != data) {
            final String path = data.getStringExtra(CameraActivity.PATH);
            final long size = data.getLongExtra(CameraActivity.SIZE, -1);
            final int width = data.getIntExtra(CameraActivity.WIDTH, -1);
            final int height = data.getIntExtra(CameraActivity.HEIGHT, -1);
            if (TextUtils.isEmpty(path)) {
                // ToastUtil.showToast("出现未知错误");
                return;
            }
            //  LogUtil.e("photo", "path = " + path + ", size = " + size + ", width = " + width + ", height = " + height);

            AlbumPhotoBean model = new AlbumPhotoBean();
            model.setPicture(path);
            model.setWidth(width);
            model.setHeight(height);
            model.setSize(size);

            final ArrayList<AlbumPhotoBean> list = new ArrayList();
            list.add(model);

            final RecyclerView recycler = findViewById(R.id.mine_feedback_picture);
            getPresenter().uploadPhoto(this, recycler, list, -1);
        }
    }
}